package com.example.administrator.financialmanager;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.example.administrator.financialmanager.dao.Outaccountdao;
import com.example.administrator.financialmanager.model.Tb_outaccount;

import java.util.Calendar;
import java.util.UUID;

public class AddOutcomeActivity extends AppCompatActivity {
    protected static final int DATE_DIALOG_ID = 0;// 创建日期对话框常量
    EditText add_outcome_money_input, add_outcome_time_input, add_outcome_position_input, add_outcome_describe_input;// 创建4个EditText对象
    Spinner add_outcome_kind_input;// 创建Spinner对象
    Button add_outcome_admit;// 创建Button对象“保存”
    Button add_outcome_cancel;// 创建Button对象“取消”

    private int mYear;// 年
    private int mMonth;// 月
    private int mDay;// 日

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_outcome);

        //获取输入控件
        add_outcome_money_input = findViewById(R.id.add_outcome_money_input);
        add_outcome_time_input = findViewById(R.id.add_outcome_time_input);
        add_outcome_position_input = findViewById(R.id.add_outcome_position_input);
        add_outcome_describe_input = findViewById(R.id.add_outcome_describe_input);
        add_outcome_kind_input = findViewById(R.id.add_outcome_kind_input);
        add_outcome_admit = findViewById(R.id.add_outcome_admit);
        add_outcome_cancel = findViewById(R.id.add_outcome_cancel);

        add_outcome_time_input.setOnClickListener(new View.OnClickListener() {// 为时间文本框设置单击监听事件
            @Override
            public void onClick(View view) {
                showDialog(DATE_DIALOG_ID);// 显示日期选择对话框
            }
        });
        
        add_outcome_admit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String money = add_outcome_money_input.getText().toString();
                if (!money.isEmpty()) {
                    Outaccountdao outaccountdao = new Outaccountdao(AddOutcomeActivity.this);
                    Tb_outaccount tb_outaccount = new Tb_outaccount(
                            UUID.randomUUID().toString(),
                            Double.parseDouble(money),
                            add_outcome_time_input.getText().toString(),
                            add_outcome_kind_input.getSelectedItem().toString(),
//                            add_outcome_kind_input.getText().toString(),
//                            add_outcome_kind_input.getSelectedItem().toString(),
                            add_outcome_position_input.getText().toString(),
                            add_outcome_describe_input.getText().toString());
                    outaccountdao.add(tb_outaccount);
                    Toast.makeText(AddOutcomeActivity.this, "〖新增支出〗数据添加成功！",Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(AddOutcomeActivity.this, "请输入支出金额！",
                            Toast.LENGTH_SHORT).show();
                }
            }
        });
        add_outcome_cancel.setOnClickListener(new View.OnClickListener() {// 为取消按钮设置监听事件

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                add_outcome_money_input.setText("");// 设置金额文本框为空
                add_outcome_money_input.setHint("0.00");// 为金额文本框设置提示
                add_outcome_time_input.setText("");// 设置时间文本框为空
                add_outcome_time_input.setHint("2011-01-01");// 为时间文本框设置提示
                add_outcome_position_input.setText("");// 设置地点文本框为空
                add_outcome_describe_input.setText("");// 设置备注文本框为空
                add_outcome_kind_input.setSelection(0);// 设置类别下拉列表默认选择第一项
            }
        });
        final Calendar c = Calendar.getInstance();// 获取当前系统日期
        mYear = c.get(Calendar.YEAR);// 获取年份
        mMonth = c.get(Calendar.MONTH);// 获取月份
        mDay = c.get(Calendar.DAY_OF_MONTH);// 获取天数

        updateDisplay();// 显示当前系统时间
    }
    @Override
    protected Dialog onCreateDialog(int id){// 重写onCreateDialog方法
        switch (id) {
            case DATE_DIALOG_ID:// 弹出日期选择对话框
                return new DatePickerDialog(this, mDateSetListener, mYear, mMonth,
                        mDay);
        }
        return null;
    }

    private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
        public void onDateSet(DatePicker view, int year, int monthOfYear,
                              int dayOfMonth) {
            mYear = year;// 为年份赋值
            mMonth = monthOfYear;// 为月份赋值
            mDay = dayOfMonth;// 为天赋值
            updateDisplay();// 显示设置的日期
        }
    };
    private void updateDisplay() {
        // 显示设置的时间
        add_outcome_time_input.setText(new StringBuilder().append(mYear).append("-")
                .append(mMonth + 1).append("-").append(mDay));
    }
}
